package com.cruisecontrol4eclipse.core;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import com.cruisecontrol4eclipse.core.model.CruiseControlProject;
import com.cruisecontrol4eclipse.core.model.CruiseControlProjectColor;
import com.cruisecontrol4eclipse.core.model.CruiseControlServer;
import com.cruisecontrol4eclipse.core.model.CruiseControlProjectColor.ProjectColor;

public class Overview extends ViewPart {
	public static final String ID = "com.cruisecontrol4eclipse.overview";
	private Image green;
	private Image red;

	private TableViewer tableViewer;
	private ProjectColor viewColor;
	
	@Override
	public void init(IViewSite site) throws PartInitException {
		green = Activator.imageDescriptorFromPlugin(
				"com.cruisecontrol4eclipse.core", "resources/green.png")
				.createImage();
		red = Activator.imageDescriptorFromPlugin(
				"com.cruisecontrol4eclipse.core", "resources/red.png")
				.createImage();
		super.init(site);
	}

	@Override
	public Image getTitleImage() {
		if(viewColor != null && viewColor.equals(ProjectColor.GREEN)) {
			return green;
		} 
		return red;
	}
	
	@Override
	public void createPartControl(Composite parent) {
		final List<CruiseControlProject> cruiseControlProjects = CruiseControlServer.getCruiseControlProjects(PersistedData.getHostname(), PersistedData.getRmiPort(), PersistedData.getHttpPort());
		for (CruiseControlProject cruiseControlProject : cruiseControlProjects) {
			cruiseControlProject.addPropertyChangeListener(new PropertyChangeListener(){
				@Override
				public void propertyChange(PropertyChangeEvent event) {
					tableViewer.update(event.getSource(), null);
					if(event.getPropertyName().equals("status")) {
						if(changedTitleIcon(CruiseControlProjectColor.getColor((CruiseControlProject)event.getSource()))){
							//This will get eclipse to call getTitleImage again...
							firePropertyChange(IWorkbenchPart.PROP_TITLE);
						}
					}
				}

				private boolean changedTitleIcon(ProjectColor newColor) {
					if(!newColor.equals(viewColor)){
						if(newColor.equals(ProjectColor.RED)){
							viewColor = ProjectColor.RED;
							return true;
						}
						
						for (CruiseControlProject cruiseControlProject : cruiseControlProjects) {
							if(CruiseControlProjectColor.getColor(cruiseControlProject).equals(ProjectColor.RED)){
								viewColor = ProjectColor.RED;
								return true;
							}
						}
						if(!viewColor.equals(ProjectColor.GREEN)){
							viewColor = ProjectColor.GREEN;
							return true;
						}
					}
					return false;
				}
			});
		}
		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(new GridLayout(1, false));

		tableViewer = new TableViewer(comp, SWT.FULL_SELECTION);
		tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
		tableViewer.setContentProvider(new CruiseControlProjectsContentProvider());
		tableViewer.setLabelProvider(new CruiseControlOverviewTableLabelProvider());
		tableViewer.getTable().setHeaderVisible(true);
		tableViewer.getTable().setLinesVisible(true);
		tableViewer.setInput(cruiseControlProjects);
		tableViewer.addDoubleClickListener(new IDoubleClickListener() {
			@Override
			public void doubleClick(DoubleClickEvent event) {
				CruiseControlProject project = (CruiseControlProject) ((IStructuredSelection)event.getSelection()).getFirstElement();
				
			}
		});

		TableColumn column = new TableColumn(tableViewer.getTable(), SWT.LEFT, 0);
	    column.setText("Project Name");
	    column.setWidth(400);

	    TableColumn column2 = new TableColumn(tableViewer.getTable(), SWT.LEFT, 1);
	    column2.setText("Label");
	    column2.setWidth(80);

	    TableColumn column3 = new TableColumn(tableViewer.getTable(), SWT.LEFT, 2);
	    column3.setText("Status");
	    column3.setWidth(150);

	    TableColumn column4 = new TableColumn(tableViewer.getTable(), SWT.LEFT, 3);
	    column4.setText("Last Successfull Label");
	    column4.setWidth(100);

	    TableColumn column5 = new TableColumn(tableViewer.getTable(), SWT.LEFT, 4);
	    column5.setText("Build Start time");
	    column5.setWidth(100);

	    MenuManager menuManager = new MenuManager(ID);
	    Menu contextMenu = menuManager.createContextMenu(tableViewer.getControl());
	    tableViewer.getControl().setMenu(contextMenu);
		getSite().registerContextMenu(menuManager, tableViewer);
		getSite().setSelectionProvider(tableViewer);
	}

	@Override
	public void setFocus() {
	}


}
